﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Kis2012.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            if (!User.IsInRole("Admin"))
                return RedirectToAction("Index", "Home");
            else
                return View();
        }

        //
        // GET: /Admin/Users

        public ActionResult Users()
        {
            DataStorage ds = DataStorage.GetInstance();
            System.Xml.Linq.XElement root = null;
            ds.DB.sp_get_userlist(ref root);

            var userblock = root.Element("users");
            if (userblock != null)
            {
                var userdata = userblock.Elements("userdata").Select(e => new
                {
                    UserId = e.Attribute("id").Value,
                    UserLogin = e.Attribute("userlogin").Value,
                    UserRealName = e.Attribute("userrealname").Value,
                    UserPass = e.Attribute("userpass").Value,
                    UserDept = e.Attribute("userdept").Value.ToString(),
                    UserPos = e.Attribute("userpos").Value.ToString(),
                    UserAdmin = e.Attribute("useradmin").Value,
                    UserActive = e.Attribute("useractive").Value
                });
                if (userdata != null)
                {
                    return View(userdata);
                } 
            }
            return RedirectToAction("Index", "Home");
        }

        public ActionResult UserDetails(int id)
        {
            DataStorage ds = DataStorage.GetInstance();
            System.Xml.Linq.XElement root = null;
            ds.DB.sp_get_userlist(ref root);

            var userblock = root.Element("users");
            if (userblock != null)
            {
                var userdata = userblock.Elements("userdata").Select(e => new
                {
                    UserId = e.Attribute("id").Value,
                    UserLogin = e.Attribute("userlogin").Value,
                    UserRealName = e.Attribute("userrealname").Value,
                    UserPass = e.Attribute("userpass").Value,
                    UserDept = e.Attribute("userdept").Value.ToString(),
                    UserPos = e.Attribute("userpos").Value.ToString(),
                    UserAdmin = e.Attribute("useradmin").Value,
                    UserActive = e.Attribute("useractive").Value
                });
                if (userdata != null)
                {
                    return View(userdata);
                }
            }
            return RedirectToAction("Index", "Home");
        }
    }
}
